/***********************************************************************
 * Module:  Photo.java
 * Author:  Voja
 * Purpose: Defines the Class Photo
 ***********************************************************************/

package model.entity;

import static javax.persistence.CascadeType.ALL;
import static javax.persistence.GenerationType.IDENTITY;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name = "photo")
@NamedQueries({
	@NamedQuery(name = "searchTitleOrDescription", 
		query = "SELECT p FROM Photo p WHERE p.title like :searchString OR p.description like :searchString"),
})
public class Photo implements Serializable{

	private static final long serialVersionUID = -2149118292233437997L;

	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "photo_id", unique = true, nullable = false)
    private Integer id;
	
	@Column(name = "photo_title", unique = false, nullable = true)
	private String title;
	
	@Column(name = "photo_description", unique = false, nullable = true)
	private String description;
	
	@Column(name = "photo_public", unique = false, nullable = true)
	private Boolean pub;
	
	@Column(name = "photo_tagsallowed", unique = false, nullable = true)
	private Boolean tagsAllowed;
	
	@Column(name = "photo_notesallowed", unique = false, nullable = true)
	private Boolean notesAllowed;
	
	@Column(name = "photo_commentsallowed", unique = false, nullable = true)
	private Boolean commentsAllowed;
	
	@Column(name = "photo_location", unique = false, nullable = true)
	private String location;
	
	@Column(name = "photo_width", unique = false, nullable = true)
	private Integer width;
	
	@Column(name = "photo_height", unique = false, nullable = true)
	private Integer height;
	
	@Column(name = "photo_size", unique = false, nullable = true)
	private Integer size;
	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "photo_uploaddate", unique = false, nullable = true)
	private Date uploadDate;

	@Column(name = "photo_rating")
	private Double rating;
	
	@Column(name = "photo_thumbnailLocation")
	private String thumbnailLocation;
	
	@ManyToMany(fetch=FetchType.EAGER)
	@JoinTable(
		name="tags_on_photo",
	    joinColumns={@JoinColumn(name="photo_id", referencedColumnName="photo_id")},
	    inverseJoinColumns={@JoinColumn(name="tag_id", referencedColumnName="tag_id")})
	private Set<Tag> tags=new HashSet<Tag>();
	
	@OneToMany(cascade = { ALL }, fetch = FetchType.EAGER, mappedBy = "photo")
	private Set<Rating> ratings=new HashSet<Rating>();
	
	@OneToMany(cascade = { ALL }, fetch = FetchType.EAGER, mappedBy = "photo")
	private Set<Note> notes=new HashSet<Note>();
	
	@OneToMany(cascade = { ALL }, fetch = FetchType.EAGER, mappedBy = "photo")
	private Set<Comment> comment=new HashSet<Comment>();
	
	@ManyToMany(fetch=FetchType.EAGER)
	@JoinTable(
		name="albums_for_photo",
	    joinColumns={@JoinColumn(name="photo_id", referencedColumnName="photo_id")},
	    inverseJoinColumns={@JoinColumn(name="album_id", referencedColumnName="album_id")})
	private Set<Album> albums=new HashSet<Album>();

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Boolean getPub() {
		return pub;
	}

	public void setPub(Boolean pub) {
		this.pub = pub;
	}

	public Boolean getTagsAllowed() {
		return tagsAllowed;
	}

	public void setTagsAllowed(Boolean tagsAllowed) {
		this.tagsAllowed = tagsAllowed;
	}

	public Boolean getNotesAllowed() {
		return notesAllowed;
	}

	public void setNotesAllowed(Boolean notesAllowed) {
		this.notesAllowed = notesAllowed;
	}

	public Boolean getCommentsAllowed() {
		return commentsAllowed;
	}

	public void setCommentsAllowed(Boolean commentsAllowed) {
		this.commentsAllowed = commentsAllowed;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public Integer getWidth() {
		return width;
	}

	public void setWidth(Integer width) {
		this.width = width;
	}

	public Integer getHeight() {
		return height;
	}

	public void setHeight(Integer height) {
		this.height = height;
	}

	public Integer getSize() {
		return size;
	}

	public void setSize(Integer size) {
		this.size = size;
	}

	public Date getUploadDate() {
		return uploadDate;
	}

	public void setUploadDate(Date uploadDate) {
		this.uploadDate = uploadDate;
	}

	public Double getRating() {
		return rating;
	}

	public void setRating(Double rating) {
		this.rating = rating;
	}

	public String getThumbnailLocation() {
		return thumbnailLocation;
	}

	public void setThumbnailLocation(String thumbnailLocation) {
		this.thumbnailLocation = thumbnailLocation;
	}

	public Set<Tag> getTags() {
		return tags;
	}

	public void setTags(Set<Tag> tags) {
		this.tags = tags;
	}

	public Set<Rating> getRatings() {
		return ratings;
	}

	public void setRatings(Set<Rating> ratings) {
		this.ratings = ratings;
	}

	public Set<Note> getNotes() {
		return notes;
	}

	public void setNotes(Set<Note> notes) {
		this.notes = notes;
	}

	public Set<Comment> getComment() {
		return comment;
	}

	public void setComment(Set<Comment> comment) {
		this.comment = comment;
	}

	public Set<Album> getAlbums() {
		return albums;
	}

	public void setAlbums(Set<Album> albums) {
		this.albums = albums;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	@Override
	public String toString() {
		return "Photo [id=" + id + "]";
	}
	
	
	@Override
	public boolean equals(Object obj) {
		if(!(obj instanceof Photo))
			return false;
		return ((Photo)obj).getId().intValue()==getId().intValue();
	}
	
}